<?php

namespace App\Http\Controllers\Backend;
use App\Common\Constant;
use App\Common\JwtAuth;
use App\Common\ResponseCode;
use App\Http\Controllers\BaseController;
use App\Http\Middleware\AdminAuth;
use App\Params\Backend\UserListParam;
use App\Params\Backend\UserLoginParam;
use App\Params\Backend\UserUpdateParam;
use App\Repository\RoleRepository;
use App\Services\User\UserService;
use App\Traits\VerifyRequestParam;
use Illuminate\Http\Request;
/**
 * 管理员控制器
 *
 * @package App\Http\Controllers\H5
 * @author WangBin
 * @date 2021-06/11
 */
class UserController extends BaseController
{
    use VerifyRequestParam;

    public function __construct()
    {

    }

    /**
     * 用户登录获取 token
     *
     * @throws
     * @return
     */
    public function login()
    {
        $param = UserLoginParam::create();
        if(!in_array($param->getLoginType(), Constant::LOGIN_WAY_LIST)) {
            return $this->fail(ResponseCode::LOGIN_WAY_ERROR);
        }
        if($param->getLoginType() == Constant::LOGIN_WAY_PWD) {
            $user = UserService::getInstance()->loginByPwd($param->getUsername(), $param->getPassword());
        }
        // 生成 token
        $token = JwtAuth::getInstance()->setType('admin')->setUserId($user->user_id)->setUserName($user->name)->encode();
        // 获取用户角色可用的操作资源
        $resource = RoleRepository::getInstance()->getResourceByIds($user->role_id);
        $result = [
            'id' => $user->user_id,
            'token' => $token->getToken(),
            'name' => $user->name,
            'expired_at' => $token->getExpiredAt(),
            'resource' => $resource
        ];
        return $this->success($result);
    }

    /**
     * 获取用户信息
     *
     * @param Request $request
     * @throws
     * @return
     */
    public function get()
    {
        $user = UserService::getInstance()->get($this->verifyInteger('id'));
        return $this->success($user);
    }

    /**
     * 保存用户
     *
     * @return
     * @throws \App\Exceptions\BusinessException
     */
    public function update()
    {
        $param = UserUpdateParam::create();
        $ret = UserService::getInstance()->update($param);
        return $this->success(['user_id' => $ret]);
    }

    /**
     * 管理员列表
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getList(Request $request)
    {
        $param = UserListParam::create();
        $ret = UserService::getInstance()->getList($param);
        return $this->success($ret);
    }

    /**
     * 删除用户
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete(Request $request)
    {
        $userId = $request->input('user_id', 0);
        $ret = UserService::getInstance()->delete($userId);
        if($ret) {
            return $this->success();
        }
        return $this->fail(ResponseCode::USER_DELETE_FAIL);
    }

    /**
     * 获取用户信息初始化数据
     *
     * @return
     * @throws
     */
    public function getInit()
    {
        return $this->success(UserService::getInstance()->getInit());
    }

    /**
     * 重置密码
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\BusinessException
     */
    public function reset()
    {
        $ret = UserService::getInstance()->reset($this->verifyInteger('user_id'), $this->verifyString('password'));
        if(!$ret) {
            return $this->fail(ResponseCode::SAVE_FAIL);
        }
        return $this->success();
    }
}
